package texture;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SimpleReloadableResourceManager implements ReloadableResourceManager {
	private MetadataSerializer metadataSerializer;

	private final Map<String, ResourceManager> domainResourceManagers = new HashMap<>();
	private final Set<String> setResourceDomains = new LinkedHashSet<>();

	public SimpleReloadableResourceManager(MetadataSerializer par1MetadataSerializer) {
		this.metadataSerializer = par1MetadataSerializer;
	}

	public Resource getResource(ResourceLocation par1ResourceLocation) throws IOException {
		ResourceManager resourcemanager = this.domainResourceManagers.get(par1ResourceLocation.getResourceDomain());

		if (resourcemanager != null) {
			return resourcemanager.getResource(par1ResourceLocation);
		} else {
			throw new FileNotFoundException(par1ResourceLocation.toString());
		}
	}

	@Override
	public void reloadResources(List<ResourcePack> par1List) {
		this.clearResources();
		Iterator<ResourcePack> iterator = par1List.iterator();

		while (iterator.hasNext()) {
			ResourcePack resourcepack = iterator.next();
			this.reloadResourcePack(resourcepack);
		}

		// this.notifyReloadListeners();
	}

	private void reloadResourcePack(ResourcePack par1ResourcePack) {
		FallbackResourceManager fallbackresourcemanager;

		Iterator<String> iterator = par1ResourcePack.getResourceDomains().iterator();
		for (; iterator.hasNext(); fallbackresourcemanager.addResourcePack(par1ResourcePack)) {
			String s = iterator.next();
			this.setResourceDomains.add(s);
			fallbackresourcemanager = (FallbackResourceManager) this.domainResourceManagers.get(s);

			if (fallbackresourcemanager == null) {
				fallbackresourcemanager = new FallbackResourceManager(this.metadataSerializer);
				this.domainResourceManagers.put(s, fallbackresourcemanager);
			}
		}
	}

	private void clearResources() {
		domainResourceManagers.clear();
	}
}
